import 'package:dio/dio.dart';
import 'package:flutterDemo/http/dio_instance.dart';
import 'package:flutterDemo/repository/datas/auth_data.dart';
import 'package:flutterDemo/repository/datas/common_website_data.dart';
import 'package:flutterDemo/repository/datas/home_banner_data.dart';
import 'package:flutterDemo/repository/datas/home_list_data.dart';
import 'package:flutterDemo/repository/datas/search_hot_key_data.dart';

class Api {
  static Api instance = Api._();
  Api._();

  Future<List<HomeBannerData?>?> getBanner() async {
    // 发起 GET 请求，获取轮播图数据
    Response response = await DioInstance.instance().get(path: "banner/json");
    // 将响应数据转换为 HomeBannerData 对象
    HomeBannerListData bannerData = HomeBannerListData.fromJson(response.data);
    // 检查响应数据中的 data 字段是否为空
    return bannerData.bannerLit;
  }

  ///获取首页文章列表
  Future<List<HomeListItemData>?> getHomeList(String pageCount) async {
    Response response =
        await DioInstance.instance().get(path: "article/list/$pageCount/json");
    HomeListData homeData = HomeListData.fromJson(response.data);
    return homeData.datas;
  }

  ///获取首页置顶数据
  Future<List<HomeListItemData>?> getHomeTopList() async {
    Response response =
        await DioInstance.instance().get(path: "article/top/json");
    HomeTopListData homeData = HomeTopListData.fromJson(response.data);
    return homeData.topList;
  }

  ///获取常用网站数据
  Future<List<CommonWebsiteData>?> getWebsiteList() async {
    Response response = await DioInstance.instance().get(path: "friend/json");
    CommonWebsiteListData websiteListData =
        CommonWebsiteListData.fromJson(response.data);
    return websiteListData.websiteList;
  }

  ///获取搜索热点数据
  Future<List<SearchHotKeyData>?> getSearchHotKeys() async {
    Response response = await DioInstance.instance().get(path: "hotkey/json");
    SearchHotKeyListData searchHotKeyListData =
        SearchHotKeyListData.fromJson(response.data);
    return searchHotKeyListData.keyList;
  }

  ///注册接口
  Future<dynamic> register(
      {String? name, String? password, String? rePassword}) async {
    Response response = await DioInstance.instance().post(
        path: "user/register",
        queryParameters: {
          "username": name,
          "password": password,
          "repassword": rePassword
        });
    return response.data;
  }

  ///登录接口
  Future<AuthData> login({String? name, String? password}) async {
    Response response =
        await DioInstance.instance().post(path: "user/login", queryParameters: {
      "username": name,
      "password": password,
    });
    return AuthData.fromJson(response.data);
  }

  ///收藏接口
  Future<bool?> collect({String? id}) async {
    Response response =
        await DioInstance.instance().post(path: "lg/collect/$id/json");
    return boolCallback(response.data);
  }

  ///取消收藏接口
  Future<bool?> unCollect({String? id}) async {
    Response response = await DioInstance.instance()
        .post(path: "lg/uncollect_originId/$id/json");
    return boolCallback(response.data);
  }

  bool? boolCallback(dynamic data) {
    if (data != null && data is bool) {
      return data;
    }
    return false;
  }

  ///退出接口
  Future<bool?> logout() async {
    Response response =
        await DioInstance.instance().get(path: "user/logout/json");
    return boolCallback(response.data);
  }

  ///模拟数据同步
  Future<void> setData() async {
    Response response = await DioInstance.instance().post(path: "/user/login");
    return response.data;
  }
}
